package com.njlime.setting.controller;

import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.PageBean;
import com.njlime.core.web.validation.annotation.support.ValidateService;
import com.njlime.setting.entities.Announce;
import com.njlime.setting.entities.AnnounceCategory;
import com.njlime.setting.service.AnnounceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping(value = "set")
public class AnnounceController extends BaseController {

    @Resource
    private AnnounceService announceService;

    /**
     * 跳转公告页面
     * @return
     */
    @RequestMapping(value = "announceView")
    public ModelAndView announceView(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/announce");
        PageBean pageBean = new PageBean(Integer.MAX_VALUE);
        AnnounceCategory announceCategory = new AnnounceCategory();
        announceCategory.setType(Announce.AnnounceType.ANNOUNCE);
        modelAndView.addObject("categoryList", announceService.categoryList(pageBean,announceCategory).getMapList());
        return modelAndView;
    }

    /**
     * 跳转公告新增页面
     * @return
     */
    @RequestMapping(value = "announceAddView")
    public ModelAndView announceAddView(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/announceAdd");
        PageBean pageBean = new PageBean(Integer.MAX_VALUE);
        AnnounceCategory announceCategory = new AnnounceCategory();
        announceCategory.setType(Announce.AnnounceType.ANNOUNCE);
        modelAndView.addObject("categoryList", announceService.categoryList(pageBean,announceCategory).getMapList());
        return modelAndView;
    }

    /**
     * 跳转公告编辑页面
     * @return
     */
    @RequestMapping(value = "announceEditView")
    public ModelAndView announceEditView(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/announceEdit");
        modelAndView.addObject("info",announceService.findOne(id));
        PageBean pageBean = new PageBean(Integer.MAX_VALUE);
        AnnounceCategory announceCategory = new AnnounceCategory();
        announceCategory.setType(Announce.AnnounceType.ANNOUNCE);
        modelAndView.addObject("categoryList", announceService.categoryList(pageBean,announceCategory).getMapList());
        return modelAndView;
    }

    /**
     * 跳转公告详情页面
     * @return
     */
    @RequestMapping(value = "announceDetailView")
    public ModelAndView announceDetailView(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/announceDetail");
        modelAndView.addObject("info",announceService.findOne(id));
        return modelAndView;
    }

    /**
     * 跳转公告详情页面
     * @return
     */
    @RequestMapping(value = "announceIndexView")
    public ModelAndView announceIndexView(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/announceDetail");
        modelAndView.addObject("info",announceService.findOne(id));
        return modelAndView;
    }

    /**
     * 跳转公告列表页面
     * @return
     */
    @RequestMapping(value = "indexListView")
    public ModelAndView indexListView(HttpServletRequest request, Integer type) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/indexList");
        PageBean pageBean = new PageBean(Integer.MAX_VALUE);
        AnnounceCategory announceCategory = new AnnounceCategory();
        announceCategory.setType(type);
        modelAndView.addObject("categoryList", announceService.categoryList(pageBean,announceCategory).getMapList());
        modelAndView.addObject("type", type);
        return modelAndView;
    }

    /**
     * 跳转帮助页面
     * @return
     */
    @RequestMapping(value = "helpView")
    public ModelAndView helpView(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/help");
        PageBean pageBean = new PageBean(Integer.MAX_VALUE);
        AnnounceCategory announceCategory = new AnnounceCategory();
        announceCategory.setType(Announce.AnnounceType.HELP);
        modelAndView.addObject("categoryList", announceService.categoryList(pageBean,announceCategory).getMapList());
        return modelAndView;
    }

    /**
     * 跳转帮助新增页面
     * @return
     */
    @RequestMapping(value = "helpAddView")
    public ModelAndView helpAddView(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/helpAdd");
        PageBean pageBean = new PageBean(Integer.MAX_VALUE);
        AnnounceCategory announceCategory = new AnnounceCategory();
        announceCategory.setType(Announce.AnnounceType.HELP);
        modelAndView.addObject("categoryList", announceService.categoryList(pageBean,announceCategory).getMapList());
        return modelAndView;
    }

    /**
     * 跳转帮助编辑页面
     * @return
     */
    @RequestMapping(value = "helpEditView")
    public ModelAndView helpEditView(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/helpEdit");
        modelAndView.addObject("info",announceService.findOne(id));
        PageBean pageBean = new PageBean(Integer.MAX_VALUE);
        AnnounceCategory announceCategory = new AnnounceCategory();
        announceCategory.setType(Announce.AnnounceType.HELP);
        modelAndView.addObject("categoryList", announceService.categoryList(pageBean,announceCategory).getMapList());
        return modelAndView;
    }

    /**
     * 跳转帮助详情页面
     * @return
     */
    @RequestMapping(value = "helpDetailView")
    public ModelAndView helpDetailView(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/helpDetail");
        modelAndView.addObject("info",announceService.findOne(id));
        return modelAndView;
    }

    /**
     * 跳转帮助详情页面
     * @return
     */
    @RequestMapping(value = "helpIndexView")
    public ModelAndView helpIndexView(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/helpDetail");
        modelAndView.addObject("info",announceService.findOne(id));
        return modelAndView;
    }

    @RequestMapping(value = "categoryManage")
    public ModelAndView categoryManage(HttpServletRequest request, Integer type) {
        ModelAndView modelAndView = new ModelAndView("/sys/set/category");
        modelAndView.addObject("type", type);
        return modelAndView;
    }



    //--------------------------分类---------------------------------

    @RequestMapping("categoryList")
    @ResponseBody
    public String categoryList(PageBean pageBean, AnnounceCategory announceCategory){
        return toJsonListData(announceService.categoryList(pageBean, announceCategory));
    }

    @RequestMapping("categoryAdd")
    @ResponseBody
    public String categoryAdd(AnnounceCategory announceCategory){
        announceCategory.setCreator(getLoginUser().getId());
        announceService.addCategory(announceCategory);
        return toJsonSuccessData();
    }

    @RequestMapping("categoryEdit")
    @ResponseBody
    public String categoryEdit(AnnounceCategory announceCategory){
        announceService.editCategory(announceCategory);
        return toJsonSuccessData();
    }

    @RequestMapping("categoryDel")
    @ResponseBody
    public String categoryDel(Integer id){
        announceService.delCategory(id);
        return toJsonSuccessData();
    }


    @RequestMapping("announceList")
    @ResponseBody
    public String announceList(PageBean pageBean, Announce announce){
        announce.setType(Announce.AnnounceType.ANNOUNCE);
        return toJsonListData(announceService.list(pageBean, announce));
    }

    @RequestMapping("announceAdd")
    @ResponseBody
    public String addAnnounce(Announce announce){
        try{
            announce.setType(Announce.AnnounceType.ANNOUNCE);
            ValidateService.valid(announce);
            announce.setCreator(getLoginUser().getId());
            announceService.add(announce);
            return toJsonSuccessData();
        }catch (Exception e){
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    @RequestMapping("announceEdit")
    @ResponseBody
    public String announceEdit(Announce announce){
        try{
            announce.setUpdater(getLoginUser().getId());
            announceService.edit(announce);
            return toJsonSuccessData();
        }catch (Exception e){
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    @RequestMapping("announceDel")
    @ResponseBody
    public String announceDel(Integer id){
        try{
            Announce ori = announceService.findOne(id);
            ori.setUpdater(getLoginUser().getId());
            announceService.del(ori);
            return toJsonSuccessData();
        }catch (Exception e){
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    @RequestMapping(value = "togglePublish", method = RequestMethod.POST)
    @ResponseBody
    public String togglePublish(HttpServletRequest request, Integer id) {
        try {
            Announce announce = announceService.findOne(id);
            announce.setUpdate_time(new Date());
            announce.setUpdater(getLoginUser().getId());
            announceService.togglePublish(announce);
            return toJsonSuccessData();
        }catch (Exception e){
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }


    @RequestMapping("helpList")
    @ResponseBody
    public String helpList(PageBean pageBean, Announce announce){
        announce.setType(Announce.AnnounceType.HELP);
        return toJsonListData(announceService.list(pageBean, announce));
    }

    @RequestMapping("helpAdd")
    @ResponseBody
    public String addHelp(Announce announce){
        try{
            announce.setType(Announce.AnnounceType.HELP);
            announce.setCreator(getLoginUser().getId());
            ValidateService.valid(announce);
            announceService.add(announce);
            return toJsonSuccessData();
        }catch (Exception e){
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    @RequestMapping("helpEdit")
    @ResponseBody
    public String helpEdit(Announce announce){
        try{
            announce.setUpdater(getLoginUser().getId());
            announce.setUpdate_time(new Date());
            announceService.edit(announce);
            return toJsonSuccessData();
        }catch (Exception e){
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    @RequestMapping("helpDel")
    @ResponseBody
    public String helpDel(Integer id){
        try{
            Announce ori = announceService.findOne(id);
            ori.setUpdater(getLoginUser().getId());
            ori.setUpdate_time(new Date());
            announceService.del(ori);
            return toJsonSuccessData();
        }catch (Exception e){
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    @RequestMapping(value = "helpPublish", method = RequestMethod.POST)
    @ResponseBody
    public String helpPublish(HttpServletRequest request, Integer id) {
        try {
            Announce announce = announceService.findOne(id);
            announce.setUpdater(getLoginUser().getId());
            announce.setUpdate_time(new Date());
            announceService.togglePublish(announce);
            return toJsonSuccessData();
        }catch (Exception e){
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }


    //=====================首页公告帮助查询================================
    @RequestMapping("indexList")
    @ResponseBody
    public String indexList(PageBean pageBean, Announce announce){
        return toJsonListData(announceService.list(pageBean, announce));
    }

}
